package com.atkxp.service;

import com.atkxp.dao.BlogMapper;
import com.atkxp.pojo.Blog;
import com.atkxp.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author user
 * @declaration
 * @create 2022/11/6-19:24
 */
@Service
public class BlogServiceImpl implements BlogService{

    @Autowired
    private BlogMapper blogMapper;
    @Override
    public List<Blog> getAllBlog() {
        return blogMapper.getAllBlog();
    }

    @Override
    public List<Blog> getAllBlogOrderByTime() {
        return blogMapper.getAllBlogOrderByTime();
    }

    @Override
    public Page<Blog> getBlogsLimit(int pageNum, int pageSize) {
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getBlogCount();
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        List<Blog> items = blogMapper.getBlogsLimit(begin, pageSize);
        page.setItem(items);
        return page;
    }

    @Override
    public Blog getBlogById(int id) {
        return blogMapper.getBlogById(id);
    }

    @Override
    public Page<Blog> getBlogByUserId(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getBlogCountLimitByUserId((int)map.get("userId"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Blog> items = blogMapper.getBlogByUserId(map);
        page.setItem(items);
        return page;
    }

    @Override
    public Page<Blog> getBlogByUserIdOrderByTime(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getBlogCountLimitByUserId((int)map.get("userId"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Blog> items = blogMapper.getBlogByUserId(map);
        page.setItem(items);
        return page;
    }

    @Override
    public int addBlog(Blog blog) {
        return blogMapper.addBlog(blog);
    }

    @Override
    public int updateBlog(Blog blog) {
        return blogMapper.updateBlog(blog);
    }

    @Override
    public int deleteBlogById(int id) {
        return blogMapper.deleteBlogById(id);
    }

    @Override
    public Page<Blog> queryBlog(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getBlogCountLimitByBlogName((String)map.get("blogName"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Blog> items = blogMapper.queryBlog(map);
        page.setItem(items);
        return page;
    }

    @Override
    public int addLikes(int id) {
        return blogMapper.addLikes(id);
    }

    @Override
    public int addBrowsesNum(int id) {
        return blogMapper.addBrowsesNum(id);
    }

    @Override
    public int deleteLikes(int id) {
        return blogMapper.deleteLikes(id);
    }

    @Override
    public Long getBlogCountLimitByUserId(int userId) {
        return blogMapper.getBlogCountLimitByUserId(userId);
    }

    @Override
    public Long getBlogCountLimitByBlogName(String blogName) {
        return blogMapper.getBlogCountLimitByBlogName(blogName);
    }

    @Override
    public Page<Blog> getBlogByTag(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getBlogCountLimitByTag((String)map.get("Tag"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Blog> items = blogMapper.getBlogByTag(map);
        page.setItem(items);
        return page;
    }

    @Override
    public List<Blog> FindSimilarBlogByTag(Blog blog) {
        return blogMapper.FindSimilarBlogByTag(blog);
    }

    @Override
    public Page<Blog> getHistoryBlogOrderByTime(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getHistoryBlogCount((int)map.get("userId"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Integer> historyBlogOrderByTime = blogMapper.getHistoryBlogOrderByTime((int)map.get("userId"));
        List<Blog> blogs = new ArrayList<>();
        for(Integer i : historyBlogOrderByTime){
            blogs.add(blogMapper.getBlogById(i));
        }
        page.setItem(blogs);
        return page;
    }

    @Override
    public int addHistoryBlog(int userId, int blogId, Date lastBrowse) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("userId", userId);
        map.put("blogId", blogId);
        map.put("lastBrowse", lastBrowse);
        return blogMapper.addHistoryBlog(map);
    }

    @Override
    public int deleteHistoryBlog(int userId, int blogId) {
        return blogMapper.deleteHistoryBlog(userId, blogId);
    }

    @Override
    public Date getHistoryBlogDate(int userId, int blogId) {
        return blogMapper.getHistoryBlogDate(userId, blogId);
    }

    @Override
    public boolean isExistHistoryBlog(int userId, int blogId) {
        if(blogMapper.isExistHistoryBlog(userId, blogId) == 1){
            return true;
        }
        return false;
    }

    @Override
    public int updateHistoryBlog(int userId, int blogId, Date lastBrowse) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("userId", userId);
        map.put("blogId", blogId);
        map.put("lastBrowse", lastBrowse);
        return blogMapper.updateHistoryBlog(map);
    }

    @Override
    public Page<Blog> getCollectedBlogOrderByTime(Map map) {
        int pageNum = (int)map.get("pageNum");
        int pageSize = (int)map.get("pageSize");
        Page<Blog> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  blogMapper.getCollectedBlogCount((int)map.get("userId"));
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        map.put("begin", begin);
        List<Integer> historyBlogOrderByTime = blogMapper.getCollectedBlogOrderByTime((int)map.get("userId"));
        List<Blog> blogs = new ArrayList<>();
        for(Integer i : historyBlogOrderByTime){
            blogs.add(blogMapper.getBlogById(i));
        }
        page.setItem(blogs);
        return page;
    }

    @Override
    public int collectBlog(int userId, int blogId, Date collectTime) {
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("blogId", blogId);
        map.put("collectTime", collectTime);
        return blogMapper.collectBlog(map);
    }

    @Override
    public int deleteCollectedBlog(int userId, int blogId) {
        return blogMapper.deleteCollectedBlog(userId, blogId);
    }

    @Override
    public boolean isCollected(int userId, int blogId) {
        if(blogMapper.isCollected(userId, blogId) == 1){
            return true;
        }
        return false;
    }

    @Override
    public int getNumOfCollections(int blogId) {
        return blogMapper.getNumOfCollections(blogId);
    }
}
